// app_info.js
//在使用的View中引入WxParse模块
var WxParse = require('../../we7/pages/wxParse/wxParse.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    app_id: '',
    logo: '',
    name: '',
    desc: '',
    qrcode: '',
    imgs: [],
    content: '',
    copyright: '',
    mobile: '',
    qrcodeDisplay: 'none',
    imgsDisplay: 'none'
  },
  /**
   * 点击二维码
   */
  qrcodeTap: function () {
    // this.setData({
    //   qrcodeDisplay: 'flex'
    // })
    wx.previewImage({
      urls: [this.data.qrcode],
    })
  },
  /**
   * 隐藏二维码蒙版
   */
  qrcodeMaskViewTap: function () {
    this.setData({
      qrcodeDisplay: 'none'
    })
  },
  /**
   * 点击二维码保存
   */
  qrcodeBgTap: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['保存到相册'],
      success: function (res) {
        if (res.tapIndex == 0) {
          wx.downloadFile({
            url: that.data.qrcode,
            success: function (res) {
              console.log(res)
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  wx.showToast({
                    title: '已保存到系统相册',
                  })
                },
                fail: function () {
                  wx.showToast({
                    title: '保存失败',
                    image: '../../resource/icon/error_icon.png',
                  })
                }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '下载失败',
                image: '../../resource/icon/error_icon.png',
              })
            }
          })
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  /**
   * 点击图片查看大图
   */
  imgTap: function (e) {
    var that = this;
    var imgIndex = 0;
    var currentImg = e.currentTarget.dataset.img;
    for (let i = 0; i < that.data.imgs.length; i++) {
      if (that.data.imgs[i] == currentImg) {
        imgIndex = i;
        break;
      }
    }
    console.log(imgIndex)
    wx.previewImage({
      current: that.data.imgs[imgIndex],
      urls: that.data.imgs
    })
  },
  /**
   * 隐藏图片蒙版
   */
  imgsMaskViewTap: function () {
    this.setData({
      imgsDisplay: 'none'
    })
  },
  /**
   * 返回首页
   */
  homeTap: function () {
    wx.navigateBack({
      delta: 3
    })
  },
  /**
   * 咨询客服
   */
  contactTap: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.mobile
    })
  },
  getAppInfo: function () {
    var that = this;
    var app_id = that.data.app_id;
    app.util.request({
      'url': 'entry/wxapp/appsdetail',
      data: {
        m: 'llt_xappsc',
        id: app_id
      },
      success: function (res) {
        console.log(res.data.data)
        var tmpData = res.data.data.appdetail
        that.setData({
          logo: tmpData.icon,
          name: tmpData.title,
          desc: tmpData.desc,
          qrcode: tmpData.qr_code,
          imgs: tmpData.thumbs,
          content: tmpData.detail,
          copyright: res.data.data.copyright,
          mobile: res.data.data.tel,
        })
        console.log(res)
        var article = that.data.content;
        WxParse.wxParse('article', 'html', article, that, 20);
        wx.setNavigationBarTitle({
          title: that.data.name,
        })
        wx.stopPullDownRefresh();
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app_id = options.id;
    this.setData({
      app_id: app_id
    })
    this.getAppInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getAppInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.name,
      path: '/pages/app_info/app_info?id=' + this.data.app_id,
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功'
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
          image: '../../resource/icon/error_icon.png'
        })
      }
    }
  }
})